/**
 * 
 */
package net.ashrentum.timer;



public class Stats {
	
	/**
	 * Number of combinations for these Stats.
	 *  
	 * Possibility to combine Stats.
	 * This variable also works as a flag indicating Stats combination or not.
	 */
	int		nStatsCombined = 0;
	
	MultipleTimeFrame mf;

	double  calls;
	double  error;

	boolean running;

	int     bettweenStops;

	
	
	int getNStatsCombined() {
		return nStatsCombined;
	}
	
	
	public Stats() {
		reset();
	}
	

	public void start() {
		mf.start();
		running = true;
	}

	public void stopBetween() {
		mf.stop();
	}

	public void stop() {
		mf.stop();
		this.running = false;
	}

	public void addCall() {
		this.calls++;
	}

	public void addError() {
		this.error++;
	}

	public void reset() {
		this.nStatsCombined = 0;
		this.calls = 0;
		this.error = 0;
		this.mf = new MultipleTimeFrame();
		this.running = false;
	}
	
	/**
	 * Combine these Stats with other Stats object.
	 * 
	 * <strong>This is a destructive method<strong>
	 * 
	 * @param o
	 * 
	 */
	public synchronized void combineStats(Stats o) {
		boolean initialVoid = (mf.getTime() == 0);
		
		this.calls = o.calls + this.calls;
		this.error = o.error + this.error;
		
		this.mf.combineTimeFrame(o.mf);
		
		if (!initialVoid && nStatsCombined == 0)
			nStatsCombined = 2;
		else
			nStatsCombined++;
	}

	public synchronized void printStats(String name) {
		System.out.println("-------------------------------------");
		System.out.println("Stats for: " + name);
		if (nStatsCombined > 0) {
			System.out.println(name + " -- combinations: " + this.nStatsCombined);			
		}
		System.out.println(name + " -- calls: " + this.calls);
		System.out.println(name + " -- time: " + this.mf.getTime() + " (" + this.mf + ")");
		System.out.println(name + " -- time/call: " + (this.mf.getTime() / this.calls));
		System.out.println(name + " -- errors: " + this.error);
		System.out.println("-------------------------------------");

	}
}
